package com.kiln.juxingqs.adapter

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import client.global.commlib.base.BaseLoadmoreAdapter
import client.global.commlib.base.BindingViewHoder
import client.global.commlib.base.SyBannerAdapter
import client.global.commlib.comm_ui.ImageGalleryActivity
import client.global.commlib.getScreenWidth
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.api.bean.Norification
import com.kiln.juxingqs.databinding.ItemSyheaderBinding
import com.kiln.juxingqs.databinding.LayoutSyitem2Binding
import com.kiln.juxingqs.ui.WebActivity
import com.kiln.juxingqs.ui.kchart.StockDetailActivity
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnBannerListener


class SyAdapter : BaseLoadmoreAdapter<GoodsIndex.Goods> {

    val manager: RequestManager
    lateinit var goodsIndex: GoodsIndex
    val keyWords = ObservableField<String>("")

    var notice_list: List<Norification.Notic>? = null

    constructor(context: Context, manager: RequestManager) : super(context,
        object : DiffUtil.ItemCallback<GoodsIndex.Goods>() {
            override fun areItemsTheSame(
                oldItem: GoodsIndex.Goods,
                newItem: GoodsIndex.Goods
            ): Boolean {
                return false
            }

            override fun areContentsTheSame(
                oldItem: GoodsIndex.Goods,
                newItem: GoodsIndex.Goods
            ): Boolean {
                return false
            }
        }) {
        this.manager = manager
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 1)
            return BindingViewHoder(
                DataBindingUtil.inflate(
                    layoutInflater,
                    R.layout.item_syheader,
                    parent,
                    false
                )
            ).apply {

                val itemBinding = this.binding as ItemSyheaderBinding

                val width = getScreenWidth(context)
                val params: ViewGroup.LayoutParams = itemBinding.banner.getLayoutParams()
                with(itemBinding) {
                    params.height = (width * 575 / 1400f).toInt()
                    banner.setLayoutParams(params)
                    banner.setIndicator(CircleIndicator(context))

                    banner2.setOnBannerListener(object : OnBannerListener<Norification.Notic> {
                        override fun OnBannerClick(b: Norification.Notic, position: Int) {

                            if (!b.img.isNullOrEmpty()) {
                                ImageGalleryActivity.show(
                                    context,
                                    b.img,
                                    true,
                                    false
                                )
                            } else if (!b.url.isNullOrEmpty()) {
                                context.startActivity(
                                    Intent(
                                        context,
                                        WebActivity::class.java
                                    ).apply {
                                        putExtra("adInfo", b.url)
                                        putExtra("title", b.text)
                                    })
                            }
                        }
                    })
                }
            }
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_syitem2,
                parent,
                false
            )
        )
    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) return 1
        return super.getItemViewType(position)
    }

    fun submitData(list: MutableList<GoodsIndex.Goods?>, goodsIndex: GoodsIndex) {
        this.goodsIndex = goodsIndex
        if (list.get(0) != null)
            list.add(0, null)
        submitListHasHeader(list)
    }

    fun submitSuperData(goodsIndex: GoodsIndex) {
        this.goodsIndex = goodsIndex
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        when (holder.itemViewType) {
            1 -> {
                val binding = bindingViewHolder.binding as ItemSyheaderBinding
                val list = goodsIndex.banner_list
                binding.banner.setAdapter(object : SyBannerAdapter<GoodsIndex.Banner>(list) {
                    override fun onBindView(
                        holder: BannerViewHolder?,
                        data: GoodsIndex.Banner?,
                        position: Int,
                        size: Int
                    ) {
                        manager.load(data?.image_url)
                            .apply(RequestOptions().error(R.mipmap.ic_launcher)).into(
                                holder!!.imageView
                            )
                    }
                })
                binding.banner.start()
                binding.event = this


//                listAction.add("在线拍卖交易，价高者得，请自行把控涨跌风险！")
//                listAction.add("精选一线顶级艺术家原作+授权限量衍生品组合商品")

//                listAction.add("甘肃省文化产权交易中心是经中宣部批复同意、甘肃省人民政府批准设立的省内唯一进行文化产权交易的国资平台机构.")
                notice_list?.let {
                    binding.banner2.setAdapter(SyNotiBannerAdapter(it))
                    binding.banner2.start()
                }
            }
            else -> {
                val binding = bindingViewHolder.binding as LayoutSyitem2Binding
//                binding.index = "$position"
                binding.event = this
                val bean = getItem(position)
                binding.bean = bean
//                manager.load(bean.goods_img)
//                    .into(binding.img)

//                binding.tvZdFloat.setTextColor(context.resources.getColor(R.color.white))
                if (bean.zd_float.startsWith("+")) {
                    binding.tvZdFloat.background = context.getDrawable(R.drawable.bg_btn_red)
                } else if (bean.zd_float.startsWith("-")) {
                    binding.tvZdFloat.background = context.getDrawable(R.drawable.bg_btn_green)
                } else {
                    binding.tvZdFloat.background = context.getDrawable(R.drawable.bg_btn_gray)
//                    binding.tvZdFloat.background = null
//                    binding.tvZdFloat.setTextColor(context.resources.getColor(R.color.txt_color))
                }

            }
        }
    }

    fun itemClick(bean: GoodsIndex.Goods) {//KlineActivity
        context.startActivity(Intent(context, StockDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
            putExtra("zd_float", "${bean.zd_float}")
        })

        /*if (getItems().indexOf(bean) == 1)
            context.startActivity(Intent(context, SyDetailActivity::class.java).apply {
                putExtra("goodsId", "${bean.goods_id}")
            })
        else {
            toast("请在开拍时间竞拍")
        }*/
    }

    fun goSearch() {
//        if (keyWords.get().isNullOrEmpty()) {
//            toast("请输入关键字")
//            return
//        }
//        context.startActivity(Intent(context, SearchRetActivity::class.java).apply {
//            putExtra("keyWords", keyWords.get())
//        })
    }
}